package com.freesun.shop.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.freesun.shop.entity.ProdProp;
import com.freesun.shop.entity.ProdPropValue;
import com.freesun.shop.mapper.ProdPropMapper;

import com.freesun.shop.mapper.ProdPropValueMapper;
import com.freesun.shop.service.ProdPropService;
import org.apache.dubbo.config.annotation.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zf
 * @since 2019-11-13
 */
@Service
public class ProdPropServiceImpl extends ServiceImpl<ProdPropMapper, ProdProp> implements ProdPropService {
    private static Logger log= LoggerFactory.getLogger(ProdPropServiceImpl.class);

    @Autowired
    private ProdPropValueMapper prodPropValueMapper;
    @Override
    public IPage<ProdProp> findByPage(ProdProp prodProp, Page<ProdProp> page) {
        log.info("商品规格分页查询,当前页:{},每页显示大小:{}",page.getCurrent(),page.getSize());
        IPage<ProdProp> iPage = this.baseMapper.selectPage(page, new LambdaQueryWrapper<ProdProp>()
                .like(StringUtils.hasText(prodProp.getPropName()), ProdProp::getPropName, prodProp.getPropName()));
        List<ProdProp> records = iPage.getRecords();
        records.forEach((prop) -> {
            List<ProdPropValue> prodPropValues = prodPropValueMapper.selectList(new LambdaQueryWrapper<ProdPropValue>()
                    .eq(ProdPropValue::getPropId, prop.getPropId()));
            prop.setProdPropValues(prodPropValues);
        });
        return iPage;
    }

    @Override
    @Transactional
    public boolean removeById(Serializable id) {
        log.info("删除属性ID为:{},信息");
        boolean falg = super.removeById(id);
        prodPropValueMapper.delete(new LambdaQueryWrapper<ProdPropValue>()
                .eq(ProdPropValue::getPropId,id));
        return falg;
    }

    @Override
    @Transactional
    public boolean save(ProdProp entity) {
        boolean falg = super.save(entity);
        List<ProdPropValue> prodPropValues = entity.getProdPropValues();
        addPropValues(entity, prodPropValues);
        return falg;
    }

    private void addPropValues(ProdProp entity, List<ProdPropValue> prodPropValues) {
        prodPropValues.forEach(prodPropValue -> {
            prodPropValue.setValueId(null);
            prodPropValue.setPropId(entity.getPropId());
            Assert.notNull(prodPropValue.getPropValue(),"属性的值不能为Null");

            prodPropValueMapper.insert(prodPropValue);
        });
    }

    @Override
    @Transactional
    public boolean updateById(ProdProp entity) {
        Assert.notNull(entity,"要修改的值不能为Null");
        Assert.notNull(entity.getPropId(),"要修改的值的ID不能为Null");
        ProdProp prodPropDb = getById(entity.getPropId());
        if(prodPropDb==null){
            throw new RuntimeException("要修改的数据不存在");
        }
        //修改prodprop的表
        boolean falg = super.updateById(entity);

        List<ProdPropValue> prodPropValuesCurrent = entity.getProdPropValues();
        //删除之前的值
        prodPropValueMapper.delete(new LambdaQueryWrapper<ProdPropValue>()
                .eq(ProdPropValue::getPropId,entity.getPropId()));
        //新增现在的值
        addPropValues(entity,prodPropValuesCurrent);
        return falg;
    }

    @Override
    public List<ProdProp> list() {
        List<ProdProp> props = super.list();
        if(null!=props){
            props.forEach(prodProp -> {
                List<ProdPropValue> prodPropValues = prodPropValueMapper.selectList(new LambdaQueryWrapper<ProdPropValue>()
                        .eq(ProdPropValue::getPropId, prodProp.getPropId()));
                prodProp.setProdPropValues(prodPropValues);
            });
        }
        return props;
    }
}
